﻿#region File Description
// EpicLeadManager
// EpicLeadManager
// PlayersListUserControl.xaml.cs
#endregion

#region Using References
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Principal;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using EpicLeadManager.Models;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

#endregion

namespace EpicLeadManager.Views.Main
{
    /// <summary>
    ///     Interaction logic for PlayersListUserControl.xaml
    /// </summary>
    public partial class PlayersListControl : UserControl, INotifyPropertyChanged
    {
        #region Fields & Properties
        private Player _playerToAdd;
        public ObservableCollection<Player> Players { get; set; }

        public Player PlayerToAdd
        {
            get { return _playerToAdd; }
            set
            {
                _playerToAdd = value;
                RaisePropertyChanged("PlayerToAdd");
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="PlayersListUserControl"/> class.
        /// </summary>
        public PlayersListControl()
        {
            Players = new ObservableCollection<Player>();
            PlayerToAdd = new Player();

            InitializeComponent();
            LoadComponents();
        }

        /// <summary>
        /// Loads the components.
        /// Concern View
        /// </summary>
        public void LoadComponents()
        {
            Players = new ObservableCollection<Player>(MainControl._PlayerSqlManager.ReadAllPlayers());

            ICollectionView view = CollectionViewSource.GetDefaultView(Players);
            if (view.CanGroup) {
                view.GroupDescriptions.Add(new PropertyGroupDescription("Profession"));
                view.SortDescriptions.Add(new SortDescription("Profession", ListSortDirection.Ascending));
                view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
            }
            Players_ListBox.DataContext = Players;
        }

        #endregion

        /// <summary>
        /// Reloads the table.
        /// </summary>
        private void ReloadTable()
        {
            Players = new ObservableCollection<Player>(MainControl._PlayerSqlManager.ReadAllPlayers());
            Players_ListBox.DataContext = Players;
        }

        #region Buttons
        private void AddPlayer_btn_OnClick(object sender, RoutedEventArgs e)
        {
            Player newPlayer = new Player("Medicant Bias", Profession.ENGINEER);


            Player p = Players.FirstOrDefault(x => x.Name == newPlayer.Name && x.Profession == newPlayer.Profession);

            if (p == null) {
                MainControl._PlayerSqlManager.AddPlayer(newPlayer);
                Players.Add(MainControl._PlayerSqlManager.LastPlayer());
            }
            else {
                MainWindow.Instance.ShowDialogMessage(String.Format("Impossible d'ajouter le joueur"), String.Format("Le joueur '{0}' est déjà présent dans la liste.", p.Name));
            }
        }


        private void DeletePlayer_btn_OnClick(object sender, RoutedEventArgs e)
        {
            Player p = Players_ListBox.SelectedItem as Player;

            if (p != null) {
                Players.Remove(p);
                MainControl._PlayerSqlManager.DeletePlayer(p);
            }

        }

        private void EditPlayer_btn_OnClick(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}